package meta.api.web.common.business;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import meta.api.web.common.dao.BusiCmnDao;
import meta.api.web.directory.businessinfo.DirInfo;
import meta.api.web.file.businessinfo.FileInfo;
import meta.api.web.file.businessinfo.MetadataInfo;
import meta.api.web.file.presentation.form.MetadataForm;

@Component
public class BusiCmnService {

    @Autowired
    private BusiCmnDao busiCmnDao;

    public int getFileTotal(MetadataInfo inputMetadata, String userId) {
        return busiCmnDao.getFileTotal(inputMetadata, userId);
    }

    public List<FileInfo> getFileList(MetadataInfo inputMetadata, String userId, Integer current, Integer pageSize) {
        return busiCmnDao.getFileList(inputMetadata, userId, current, pageSize);
    }

    public void saveFile(FileInfo fileInfo, byte[] fileContent) {
        busiCmnDao.saveFile(fileInfo, fileContent);
    }

    public FileInfo getFileByFilename(String filename, String userId) {
        FileInfo fileInfo = busiCmnDao.getFileByFilename(filename, userId);
        if (fileInfo == null) {
            return null;
        }
        fileInfo.getMetadata().rebuildTagList();
        return fileInfo;
    }

    public boolean checkIsDirOwner(String id, String userId) {
        return busiCmnDao.checkIsDirOwner(id, userId);
    }

    /**
     * 检证文件是否存在
     * 
     * @param filename 文件名
     * @param existFlg null :文件记录存在，忽略文件删除日期是否为NULL<br>
     *                 true :文件逻辑存在，文件删除日期为NULL<br>
     *                 false:文件逻辑删除，文件删除日期不为NULL
     * @return 存在的场合返回true，否则返回false。
     */
    public boolean checkIsFileExisted(String filename, Boolean existFlg) {
        return busiCmnDao.checkIsFileExisted(filename, existFlg);
    }

    public boolean checkIsFileOwner(String filename, String userId) {
        return busiCmnDao.checkIsFileOwner(filename, userId);
    }

    public boolean checkIsDataOwner(String id, String userId) {
        return busiCmnDao.checkIsDataOwner(id, userId);
    }

    public void updateFile(String filename, MetadataForm metadataForm, String userId) {
        busiCmnDao.updateFiles(Arrays.asList(filename), metadataForm, userId);
    }

    public void updateFiles(List<String> filenames, MetadataForm metadataForm, String userId) {
        busiCmnDao.updateFiles(filenames, metadataForm, userId);
    }

    public void deleteFileByFilename(String filename, String userId) {
        busiCmnDao.deleteFilesByFilenames(Arrays.asList(filename), userId);
    }

    public void deleteFilesByFilenames(List<String> filenames, String userId) {
        busiCmnDao.deleteFilesByFilenames(filenames, userId);
    }

    public void updateFilesDeletedByFilenames(List<String> filenames, String userId) {
        busiCmnDao.updateFilesDeletedByFilenames(filenames, userId);
    }

    public void updateFilesNotDeletedByFilenames(List<String> filenames, String userId) {
        busiCmnDao.updateFilesNotDeletedByFilenames(filenames, userId);
    }

    public boolean checkExistDir(String userId, String type, String fullpath) {
        return busiCmnDao.checkExistDir(userId, type, fullpath);
    }

    public void addDirInfo(String userId, String type, String fullpath) {
        busiCmnDao.addDirInfo(userId, type, fullpath);
    }

    public void updateDirInfo(String userId, String type, String oldFullpath, String newFullpath) {
        busiCmnDao.updateDirInfo(userId, type, oldFullpath, newFullpath);
    }

    public DirInfo getDirInfo(String id, String userId) {
        return busiCmnDao.getDirInfo(id, userId);
    }

    public DirInfo getDirInfo(String userId, String type, String fullpath) {
        return busiCmnDao.getDirInfo(userId, type, fullpath);
    }

    public int getDirInfoTotal(String userId, String type, String fullpath) {
        return busiCmnDao.getDirInfoTotal(userId, type, fullpath);
    }

    public List<DirInfo> getDirInfoList(String userId, String type, String fullpath) {
        return busiCmnDao.getDirInfoList(userId, type, fullpath);
    }

    public void deleteDirInfo(String id, String userId) {
        busiCmnDao.deleteDirInfo(id, userId);
    }

    public void deleteDirInfo(String userId, String type, List<String> fullpaths) {
        busiCmnDao.deleteDirInfo(userId, type, fullpaths);
    }
}
